package com.xiaoyi.encrypt;


import com.xiaoyi.enums.AlgorithmEnum;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @author xiaoyi
 * @version 1.0.0
 * @date 2023/08/06 14:52:00
 */
public class EncryptorFactory {
    private EncryptorFactory(){}
    private static final Map<AlgorithmEnum,IEncryptorStrategy>  encryptorStrategyMap = new HashMap<>();
    public static synchronized IEncryptorStrategy  createEncryptor(AlgorithmEnum algorithm ){
        IEncryptorStrategy iEncryptorStrategy = encryptorStrategyMap.get(algorithm);
        if (Objects.isNull(iEncryptorStrategy)){
            Class value = algorithm.getValue();
            try {
                iEncryptorStrategy = (IEncryptorStrategy) value.newInstance();
                encryptorStrategyMap.put(algorithm,iEncryptorStrategy);
                return iEncryptorStrategy;
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        return  iEncryptorStrategy;
    }
}
